class Solution {
    public boolean isHappy(int n) {
        int slow = n;
        int fast = getNext(n);
        while(slow != fast){
            slow = getNext(slow);
            fast = getNext(getNext(fast));
        }
        return fast == 1;
    }

    public int getNext(int n){
        int sum = 0;
        while(n != 0){
            int m = n % 10;
            sum += m*m;
            n /= 10; 
        }
        return sum;
    }
}
